class Solution:
    def rle(self, num: str) -> str:
        res = ""
        i = 1
        a, b = num[0], 1
        while i < len(num):
            if num[i] != num[i - 1]:
                res += f"{a}{b}"
                a, b = num[i], 1
            else:
                b += 1
            i += 1

        return res + f"{a}{b}"

    def countAndSay(self, n: int) -> str:
        if n == 1:
            return "1"

        return self.rle(self.countAndSay(n - 1))
